package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsData;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private LogService logService;
    @Autowired
    private GoodTypeService goodTypeService;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        // 1. 查询总数
        int total = goodsDao.getGoodsCount();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> roles = goodsDao.getGoodsList(offSet, rows, codeOrName, goodsTypeId);
        logService.save(new Log(Log.SELECT_ACTION, "分页查询商品库存信息，当前页码：" + page));
        map.put("total", total);
        map.put("rows", roles);
        return map;

    }

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        // 1. 查询总数
        int total = goodsDao.getGoodsCount();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> roles = goodsDao.getGoodsList(offSet, rows, goodsName, goodsTypeId);
        logService.save(new Log(Log.SELECT_ACTION, "分页查询供应商信息，当前页码：" + page));
        map.put("total", total);
        map.put("rows", roles);
        return map;
    }

    @Override
    public ServiceVO save(Goods goods) {
        // 商品id为空，则为新增操作
        if (goods.getGoodsId() == null) {
            Goods exGoods = goodsDao.findGoodsByName(goods.getGoodsName());
            if (exGoods != null) {
                return new ServiceVO<>(ErrorCode.Goods_Exist_Code, ErrorCode.Goods_Exist_MESS);
            }
            logService.save(new Log(Log.INSERT_ACTION, "新增商品:" + goods.getGoodsId()));

            GoodsData goodsData = new GoodsData();
            goodsData.setGoodsId(0);
            goodsData.setGoodsCode(goods.getGoodsCode());
            goodsData.setGoodsName(goods.getGoodsName());
            goodsData.setInventoryQuantity(0);// 库存数量 默认为0
            goodsData.setMinNum(0);// 最小起订量默认为0
            goodsData.setGoodsModel(goods.getGoodsModel());
            goodsData.setGoodsProducer(goods.getGoodsProducer());
            goodsData.setPurchasingPrice(goods.getPurchasingPrice());
            goodsData.setLastPurchasingPrice(goods.getPurchasingPrice());
            goodsData.setRemarks(goods.getRemarks());
            goodsData.setSellingPrice(goods.getSellingPrice());
            goodsData.setState(0);// 商品状态默认为0
            goodsData.setGoodsUnit(goods.getGoodsUnit());
            goodsData.setGoodsTypeId(goods.getGoodsTypeId());
            goodsDao.insertGoods(goodsData);
        } else {
            logService.save(new Log(Log.UPDATE_ACTION, "修改商品:" + goods.getGoodsName()));
            GoodsData goodsData = new GoodsData();
            goodsData.setGoodsId(goods.getGoodsId());
            goodsData.setGoodsCode(goods.getGoodsCode());
            goodsData.setGoodsName(goods.getGoodsName());
//            goodsData.setInventoryQuantity(0);
            goodsData.setMinNum(goods.getMinNum());
            goodsData.setGoodsModel(goods.getGoodsModel());
            goodsData.setGoodsProducer(goods.getGoodsProducer());
            goodsData.setPurchasingPrice(goods.getPurchasingPrice());
            goodsData.setLastPurchasingPrice(goods.getLastPurchasingPrice());
            goodsData.setRemarks(goods.getRemarks());
            goodsData.setSellingPrice(goods.getSellingPrice());
            goodsData.setGoodsUnit(goods.getGoodsUnit());
            goodsData.setGoodsTypeId(goods.getGoodsTypeId());
            goodsDao.updateGoods(goodsData, goods.getGoodsId());
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(Integer goodsId) {
        Integer state = goodsDao.selectState(goodsId);
        switch (state) {
            case 2:
                return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE, ErrorCode.HAS_FORM_ERROR_MESS);
            case 1:
                return new ServiceVO<>(ErrorCode.STORED_ERROR_CODE, ErrorCode.STORED_ERROR_MESS);
        }
        goodsDao.delete(goodsId);
        logService.save(new Log(Log.DELETE_ACTION, "删除商品:" + goodsId));
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        //无库存商品列表展示（可以根据商品名称或编码查询）
        Map<String, Object> map = new HashMap<>();
        // 1. 查询总数
        int total = goodsDao.getNoInventoryQuantityCount();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        // 2. 根据总数和当前页码查询商品列表
        List<Goods> roles = goodsDao.getNoInventoryQuantity(offSet, rows, nameOrCode);
        logService.save(new Log(Log.SELECT_ACTION, "分页查询无库存商品信息，当前页码：" + page));
        map.put("total", total);
        map.put("rows", roles);
        return map;
    }

    @Override
    public ServiceVO saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        // 添加库存、修改数量或成本价
        if (inventoryQuantity != null) {
            goodsDao.updateInventory(goodsId, inventoryQuantity);
        }
        if (purchasingPrice != 0) {
            // 并更新上一次采购价格lastPurchasingPrice
            // 1. 先更新上一次采购价格lastPurchasingPrice再更新采购价格purchasingPrice
            goodsDao.updatePurchasingAndLastPurchasing(goodsId, purchasingPrice);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> listAlarm() {
        // 查询库存报警商品列表
        List<Goods> list = goodsDao.listAlarm();
        logService.save(new Log(Log.SELECT_ACTION, "查询库存报警商品列表"));
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

}


